// 掌心雷
// @author:wangff8@qq.com  @date:2024-11-01
library SimulateAttac4 initializer init requires SimulateAttack

    globals

        // 掌心雷基础伤害  攻击力*2
        // 雷伤害  黄色

        // 掌心雷等级(默认0)
        integer array ct_zxlLevel
        // 掌心雷CD(默认5秒)
        real array ct_zxlCD
        // 掌心雷爆炸范围（默认200）
        real array ct_zxlBoomRang
        // 掌心雷爆炸范围增幅（默认0%）（最大200%也就是600码）
        real array ct_zxlBoomRangZf
        // 掌心雷爆炸伤害增幅（默认0%）
        real array ct_zxlBoomDmgZf
        // 掌心雷连放次数(默认0)
        integer array ct_zxlAttCount
        // 掌心雷击中是否触发麻痹(默认不触发)
        boolean array ct_zxlIsMB
        // 掌心雷麻痹概率（基础默认20%）
        real array ct_zlxMbRate
        // 掌心雷麻痹概率增幅（默认0%）
        real array ct_zlxMbRateZf
        // 掌心雷麻痹等级(总共10个等级，默认1，没升级一次增加0.5秒)
        integer array ct_zlxMBLevel

    endglobals

    private function init takes nothing returns nothing
        local integer uid = 0
        loop
            exitwhen uid > PLAYER_NUMBER
            set ct_zxlLevel[uid] = 0
            set ct_zxlCD[uid] = 5.0
            set ct_zxlBoomRang[uid] = 200.0
            set ct_zxlBoomRangZf[uid] = 0.0
            set ct_zxlBoomDmgZf[uid] = 0.0
            set ct_zxlAttCount[uid] = 0
            set ct_zxlIsMB[uid] = false
            set ct_zlxMbRate[uid] = 0.2
            set ct_zlxMbRateZf[uid] = 0.0
            set ct_zlxMBLevel[uid] = 1
            set uid = uid + 1
        endloop
    endfunction

    // 掌心雷麻痹单位
    private function maBi takes integer uid, unit bu returns nothing
        local integer rate = R2I(ct_zlxMbRate[uid] * (1.0 + ct_zlxMbRateZf[uid]) * 100.0)

        local unit majia
        if GetRandomInt(1, 100) <= rate then
            // 创建马甲单位
            set majia = CreateUnit(Player(uid), 'N001', GetUnitX(bu), GetUnitY(bu), 0.0)
            // 设置马甲的生命周期
            call UnitApplyTimedLife(majia, 'Ahwe', 2.0)
            // 设置技能等级
            if ct_zlxMBLevel[uid] > 10 then
                set ct_zlxMBLevel[uid] = 10
            endif
            call SetUnitAbilityLevel(majia, 'A005', ct_zlxMBLevel[uid])
            // 释放冻结
            call IssueTargetOrder(majia, "thunderbolt", bu)
        endif

        set majia = null
    endfunction

    // 掌心雷命中爆炸
    // 掌心雷中怪物后对100码范围造成攻击*2.0的伤害
    private function boom takes integer uid, real x, real y returns nothing
        local group g = CreateGroup()
        local boolexpr filter
        local unit ydl_unit
        local unit u = udg_player_select_hero[uid]
        local effect ect
        local real shv = FF_GetAttack(u) * 2.0 * (ct_zxlBoomDmgZf[uid] + 1.0)
        // 范围增幅
        local real zf = ct_zxlBoomRangZf[uid] + 1.0

        // 特效 
        if tx_show_status[uid] == 1 then
            set ect = AddSpecialEffect("hs_shandian.mdx", x, y)
            call EXSetEffectSize(ect, 1.0 * zf)
            call DestroyEffect(ect)
        endif

        set filter = Condition(function heroSkillConditions)
        call GroupEnumUnitsInRange(g, x, y, ct_zxlBoomRang[uid] * zf, filter)
        loop
            set ydl_unit = FirstOfGroup(g)
            exitwhen (ydl_unit == null)
            call GroupRemoveUnit(g, ydl_unit)
            call UnitDamageTarget( u, ydl_unit, shv, true, true, ATTACK_TYPE_NORMAL, DAMAGE_TYPE_LIGHTNING, WEAPON_TYPE_WHOKNOWS )

            // 如果还有生命值，是否麻痹
            if FF_GetHp(ydl_unit) >= 1.0 and ct_zxlIsMB[uid] then
                call maBi(uid, ydl_unit)
            endif
        endloop

        call DestroyBoolExpr(filter)
        call DestroyGroup(g)

        set ect = null
        set u = null
        set g = null
        set filter = null
        set ydl_unit = null
    endfunction


    // 在释放一次
    function timer_releseZXL takes nothing returns nothing
        local timer t = GetExpiredTimer()
        local integer tid = GetHandleId(t)
        local integer uid = LoadInteger(gameht, tid, 1)
        local unit bu = getNearestUnit(uid)
        local integer count = LoadInteger(gameht, tid, 6) + 1

        // 如果敌人距离英雄只有1600码的时候攻击第一个
        if getDistanceUnit(udg_player_select_hero[uid], bu) > 1600 then
            set bu = getRandomUnit(uid)
        endif

        call SaveInteger(gameht, tid, 6, count)
        call boom(uid, GetUnitX(bu), GetUnitY(bu))
        if count >= ct_zxlAttCount[uid] then
            call FlushChildHashtable(gameht, tid)
            call DestroyTimer(t)
        endif

        set t = null
        set bu = null
    endfunction

    // 释放掌心雷
    function releseZXL takes integer uid returns nothing
        local unit bu = getNearestUnit(uid)
        local timer t
        local integer tid

        // 如果敌人距离英雄只有1600码的时候攻击第一个
        if getDistanceUnit(udg_player_select_hero[uid], bu) > 1600 then
            set bu = getRandomUnit(uid)
        endif
        
        call boom(uid, GetUnitX(bu), GetUnitY(bu))

        if ct_zxlAttCount[uid] > 0 then
            set t = CreateTimer()
            set tid = GetHandleId(t)

            call SaveInteger(gameht, tid, 1, uid)

            call TimerStart(t, 0.5, true, function timer_releseZXL)
        endif

        set bu = null
        set t = null
    endfunction
    
endlibrary



